﻿#include "getcommons.h"

GetCommons::GetCommons(QObject *parent) : QObject(parent)
{

}

GetCommons::~GetCommons()
{

}


void GetCommons::readConfigFile(QString configFile)
{
    // 将xml文件读取数据
    QFile file(configFile);

    if (!file.open(QFile::ReadOnly | QFile::Text))
    {
      qDebug()<<file.errorString();
      return;
    }

    QXmlStreamReader reader;

    // 指定读取设备
    reader.setDevice(&file);

    while(!reader.atEnd())
    {
        // 读取下一个标签
        reader.readNext();
        // 解析内容
        if(reader.isStartElement())
        {
            // 解析PLT_REQUEST_MSG参数
            if(reader.name() == QString("PLT_REQUEST_MSG"))
            {
                commons.PLT_REQUEST_MSG = reader.readElementText().toInt(NULL, 16);
                continue;
            }

            // 解析PLT_MSG_TYPE参数
            if(reader.name() == QString("PLT_MSG_TYPE"))
            {
                commons.PLT_MSG_TYPE = reader.readElementText().toInt(NULL, 16);
                continue;
            }

            // 解析WITHOUT_TITLE参数
            if(reader.name() == QString("WITHOUT_TITLE"))
            {
                if(reader.readElementText().compare("True"))
                {
                    commons.WITHOUT_TITLE = true;
                    continue;
                }

                if(reader.readElementText().compare("False"))
                {
                    commons.WITHOUT_TITLE = false;
                    continue;
                }

                QMessageBox::warning(NULL, QString("错误提示"), QString("配置文件\"") + configFile + QString("\"中WITHOUT_TITLE参数的值有误,必须为True或者False!"));
            }

            // 解析SYSTEM_CYCLE参数
            if(reader.name() == QString("SYSTEM_CYCLE"))
            {
                commons.SYSTEM_CYCLE = reader.readElementText().toFloat();
            }

            // 解析DEFAULT_WIDTH参数
            if(reader.name() == QString("DEFAULT_WIDTH"))
            {
                commons.DEFAULT_WIDTH = reader.readElementText().toInt();
            }

            // 解析DEFAULT_HEIGHT参数
            if(reader.name() == QString("DEFAULT_HEIGHT"))
            {
                commons.DEFAULT_HEIGHT = reader.readElementText().toInt();
            }

            // 解析COLUMNS_PER_ROW参数
            if(reader.name() == QString("COLUMNS_PER_ROW"))
            {
                commons.COLUMNS_PER_ROW = reader.readElementText().toInt();
            }

            // 解析ROWS_PER_COLUMN参数
            if(reader.name() == QString("ROWS_PER_COLUMN"))
            {
                commons.ROWS_PER_COLUMN = reader.readElementText().toInt();
            }

            // 解析HEIGHT_PERCENT参数
            if(reader.name() == QString("HEIGHT_PERCENT"))
            {
                commons.HEIGHT_PERCENT = reader.readElementText().toInt();
            }

            // 解析HORIZONTAL_SIDE_SPACING参数
            if(reader.name() == QString("HORIZONTAL_SIDE_SPACING"))
            {
                commons.HORIZONTAL_SIDE_SPACING = reader.readElementText().toInt();
            }

            // 解析VERTICAL_SIDE_SPACING参数
            if(reader.name() == QString("VERTICAL_SIDE_SPACING"))
            {
                commons.VERTICAL_SIDE_SPACING = reader.readElementText().toInt();
            }

            // 解析DRAW_AREA_WIDTH_PERCENT参数
            if(reader.name() == QString("DRAW_AREA_WIDTH_PERCENT"))
            {
                commons.DRAW_AREA_WIDTH_PERCENT = reader.readElementText().toInt();
            }

            // 解析RULER_AREA_HEIGHT参数
            if(reader.name() == QString("RULER_AREA_HEIGHT"))
            {
                commons.RULER_AREA_HEIGHT = reader.readElementText().toInt();
            }

        }
    }
    file.close();

    return;
}


void GetCommons::getConfig(pCOMMONS pCommons)
{
    if(NULL == pCommons) return;

    pCommons->PLT_REQUEST_MSG = commons.PLT_REQUEST_MSG;
    pCommons->PLT_MSG_TYPE = commons.PLT_MSG_TYPE;
    pCommons->COLUMNS_PER_ROW = commons.COLUMNS_PER_ROW;
    pCommons->DEFAULT_HEIGHT = commons.DEFAULT_HEIGHT;
    pCommons->DEFAULT_WIDTH = commons.DEFAULT_WIDTH;
    pCommons->DRAW_AREA_WIDTH_PERCENT = commons.DRAW_AREA_WIDTH_PERCENT;
    pCommons->HEIGHT_PERCENT = commons.HEIGHT_PERCENT;
    pCommons->HORIZONTAL_SIDE_SPACING = commons.HORIZONTAL_SIDE_SPACING;
    pCommons->ROWS_PER_COLUMN = commons.ROWS_PER_COLUMN;
    pCommons->SYSTEM_CYCLE = commons.SYSTEM_CYCLE;
    pCommons->VERTICAL_SIDE_SPACING = commons.VERTICAL_SIDE_SPACING;
    pCommons->WITHOUT_TITLE = commons.WITHOUT_TITLE;
    pCommons->RULER_AREA_HEIGHT = commons.RULER_AREA_HEIGHT;

    return;
}
